package heap;

import java.util.*;

/**
 * 最后一块石头的重量
 */
public class WeightLastStone {
    public int lastStoneWeight(int[] stones) {
        // 最大堆
        Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int stone : stones) {
            queue.offer(stone);
        }
        while (queue.size() > 1) {
            int a = queue.poll();
            int b = queue.poll();
            if (a != b) {
                queue.offer(a - b);
            }
        }
        if (queue.size() == 0) {
            return 0;
        }
        return queue.poll();
    }
}
